﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Communergy.Common;
using Communergy.Common.Events;
using System.Threading;

namespace CurrentCostLibrary
{
    public class RandomDataMonitorProvider : IEnergyMonitorProvider
    {

        int pauseSeconds = 6;
        static int min = 50;
        static Int32 max = 2000;
        Thread randomDataThread;
        private static Random random = new Random((int)DateTime.Now.Ticks);
        DateTime dateStarted = DateTime.Now;
        bool sendData = false;
        object thisLock = new object();

        public RandomDataMonitorProvider()
        {
                
        }

        public RandomDataMonitorProvider(int pauseSeconds)
        {
            this.pauseSeconds = pauseSeconds;
        }

        #region IEnergyMonitorProvider Members




        public event NewEnergyReadingHandler EnergyReading;

        public void StartReadings()
        {
            sendData = true;
            randomDataThread = new Thread(() =>
                               {
                                   do
                                   {                                      
                                       Thread.Sleep(pauseSeconds * 1000);
                                       if (sendData)
                                       {
                                           var output = CreateEnergyMonitorOutputWithRandomData();
                                           RaiseNewReadingEvent(new EnergyReadingEventArgs { EnergyMonitorOutput = output });
                                       }
                                   }
                                   while (true);
                               });
            randomDataThread.Start();
        }


        private void RaiseNewReadingEvent(EnergyReadingEventArgs re)
        {
            if (EnergyReading != null)
            {
                EnergyReading(this, re);
            }
        }

        public void EndReadings()
        {

            sendData = false;
        }

        #endregion        
        
        

        private EnergyMonitorOutput CreateEnergyMonitorOutputWithRandomData()
        {
            var startDateTime = DateTime.Now;

            var output = new EnergyMonitorOutput();
            output.DateOfBirth = dateStarted;
            output.MeterId = "Random data generator";
            output.Source = "Random";
            output.SensorReadings = new List<EnergyMonitorSensorReading>();
            output.ReadingDateTime = startDateTime;
            
            

            for (int sensorId = 0; sensorId < 10; sensorId++)
            {
                var sensor = CreateSensorReading(startDateTime, sensorId);
                output.SensorReadings.Add(sensor); 
            }

            return output;
        }

        private EnergyMonitorSensorReading CreateSensorReading(DateTime readingDateTime,  int sensorId)
        {
            var sensor = new EnergyMonitorSensorReading();

            var randomWatts = random.Next(min, max);
            var newReading = new EnergyData { ReadingDateTime = readingDateTime, AverageWatts = randomWatts, Interval = new TimeSpan(0, 0, pauseSeconds) };
            var newReadings = new List<EnergyData>();
            newReadings.Add(newReading);

            sensor.SensorId = sensorId;
            sensor.ReadingDateTime = readingDateTime;
            sensor.LiveReadings = newReadings;
            return sensor;
        }

        public static int RandomInt(int min, int max)
        {
            return random.Next(min, max);
        }


    }
}
